<?php

class exportdataTask extends sfBaseTask {

    protected function configure() {
        // // add your own arguments here
        $this->addArguments(array(
            new sfCommandArgument('subject-tags', sfCommandArgument::IS_ARRAY, 'The list of subject tags collection items must match to be exported'),
        ));

        $this->addOptions(array(
            new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name','frontend'),
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
            new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
            // add your own options here
            new sfCommandOption('modified-since', null, sfCommandOption::PARAMETER_OPTIONAL, 'Only collection items modified after this date will be exorted (Format: YYYY-MM-DD HH24:MI:SS)'),
            new sfCommandOption('export-format', null, sfCommandOption::PARAMETER_REQUIRED, 'The format to export the data in [travide|rifcs]', 'travide'),
            new sfCommandOption('export-file', null, sfCommandOption::PARAMETER_REQUIRED, 'The filename to export too', 'export.xml'),
            new sfCommandOption('key-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'Key prefix to append to the collection UINs (Format: RIF-CS only)'),
            new sfCommandOption('registry-object-group', null, sfCommandOption::PARAMETER_REQUIRED, 'The RIF-CS registry object group to put records in (Format: RIF-CS only)'),
            new sfCommandOption('url-location-pattern', null, sfCommandOption::PARAMETER_REQUIRED, 'The pattern to create the URL\'s for RIF-CS records (Format: RIF-CS only)'),
            new sfCommandOption('default-originating-source', null, sfCommandOption::PARAMETER_REQUIRED, 'The default originating source to append to records'),
            new sfCommandOption('export-resources', null, sfCommandOption::PARAMETER_REQUIRED, 'If to export resources for travide formats',false),
            new sfCommandOption('resource-base-path', null, sfCommandOption::PARAMETER_REQUIRED, 'Path to export resources too'),
            new sfCommandOption('min-visibility-level', null, sfCommandOption::PARAMETER_REQUIRED, 'The minimum visibility level collection items must be at to be included in the export'),
        ));

        $this->namespace = 'travide';
        $this->name = 'export-data';
        $this->briefDescription = 'Exports Collection Item Data in XML format';
        $this->detailedDescription = <<<EOF
The [export-data|INFO] task does things.
Call it with:

  [php symfony export-data|INFO]
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

        /**
         * First get the list of collections using the supplied limiters
         * of subject tags and date modififed
         *
         */
        $this->logSection('init', 'building SQL');
        $collectionQuery = Doctrine::getTable('CollectionItem')->getCollectionsByTagNamesQuery($arguments['subject-tags']);
        /**
         * Only export collections modified after the specified date
         */
        if ($options['modified-since']) {
            $collectionQuery = Doctrine::getTable('CollectionItem')->addCollectionsModifiedAfterDate($options['modified-since'],$collectionQuery);
        }

        if ($options['min-visibility-level']) {
            $collectionQuery = Doctrine::getTable('CollectionItem')->addSearchByMinimumVisibilityLevel($options['min-visibility-level'],$collectionQuery);
        }
        
        $this->logSection('init', 'running SQL');
        $collectionsList = $collectionQuery->execute();

        if ($options['export-format'] == 'travide') {
            $this->logSection('export', 'exporting in ' . $options['export-format'] . ' format');

            $exporter = new ExportGeneratorTravide();
            $exporter->setExportResources($options['export-resources']);
            $exporter->setResourceBasePath($options['resource-base-path']);

            $exporter->generateExportFromCollections($collectionsList)->save($options['export-file']);
            $this->logSection('export', 'export completed');
        } else if ($options['export-format'] == 'rifcs') {
            $this->logSection('export', 'exporting in ' . $options['export-format'] . ' format');

            $exporter = new ExportGeneratorRIFCS();

            $exporter->setRegistryObjectGroup($options['registry-object-group']);
            $exporter->setDefaultOriginatingSource($options['default-originating-source']);
            $exporter->setKeyPrefix($options['key-prefix']);
            $exporter->setRifcsTypeMappingListFromDoctrine(Doctrine::getTable('RifCollTypeMapping')->findAll());
            $exporter->setUrlLocationPattern($options['url-location-pattern']);


            $exporter->generateExportFromCollections($collectionsList)->save($options['export-file']);
            $this->logSection('export', 'export completed');
        } else {
            $this->logSection('export', 'Error unknown format: ' . $options['export-format']);
        }
    }

}
